// 请求封装
"use strict";
var aServer = {
	// ApiUrl: "http://192.168.1.188:8889/tp-api/",
	ResUrl: "http://150.139.220.225:8080/tp-api/filePath",
	ApiUrl: "http://150.139.220.225:8282", //正式环境
	//测试环境
	// ApiUrl: "http://192.168.2.207:8080", //测试环境
	// ApiUrl: "http://192.168.50.154:8080", //测试环境 家
	// ApiUrl: "http://192.168.1.155:8080", //tp
	
	
	executeActionOfServer: function(isDebug, methodName, errMsg, postData, success, error) {
		aUtil.aLog.deb(isDebug, this.ApiUrl);
		// aUtil.aLog.deb(isDebug, methodName + " postData is " + JSON.stringify(postData));
		var success4Post = function(result, status, xhr) {
			if (!result) {
				aUtil.aLog.err(isDebug, errMsg);
				if (error) error(errMsg);
			} else {
				aUtil.aLog.deb(isDebug, methodName + " return is " + JSON.stringify(result));
				if (success) {
					success(result);
				} else {
					mui.toast('登录失败');
					if (error) error(errMsg);
				}
			}
		};
		console.log(this.ApiUrl + methodName);
		mui.ajax(this.ApiUrl + methodName, {
			data: JSON.stringify(postData),
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 15000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json',
				'Authorization': "Bearer " + LocalStorage.getItem(LocalStorage.keys.Auth_Token),
			},
			beforeSend: function(xhr, setting) {},
			complete: function(xhr, status) {

			},
			success: success4Post,
			error: function(xhr, type, errorThrown) {
				//				mui.toast("服务器异常：" + type);
				if (error) error(xhr.responseText);
				console.log("服务器异常" + xhr.status + "  " + xhr.readyState + xhr.responseText);
			}
		});
	},
	
	executeActionOfServerNoToken: function(isDebug, methodName, errMsg, postData, success, error) {
		aUtil.aLog.deb(isDebug, this.ApiUrl);
		aUtil.aLog.deb(isDebug, methodName + " postData is " + JSON.stringify(postData));
	
		var success4Post = function(result, status, xhr) {
			if (!result) {
				aUtil.aLog.err(isDebug, errMsg);
				if (error) error(errMsg);
			} else {
				aUtil.aLog.deb(isDebug, methodName + " return is " + JSON.stringify(result));
				if (success) {
					success(result);
				} else {
					mui.toast('登录失败');
					if (error) error(errMsg);
				}
			}
		};
		// postData["jstime"] = new Date().getTime();
		console.log(this.ApiUrl + methodName);
		mui.ajax(this.ApiUrl + methodName, {
			data: JSON.stringify(postData),
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 15000, //超时时间设置为10秒；
			headers: {
				'Content-Type':'application/x-www-form-urlencoded',
			},
			beforeSend: function(xhr, setting) {},
			complete: function(xhr, status) {
	
			},
			success: success4Post,
			error: function(xhr, type, errorThrown) {
				if (error) error(xhr.responseText);
				console.log("服务器异常" + xhr.status + "  " + xhr.readyState + xhr.responseText);
			}
		});
	},

	executeActionOfServerGet: function(isDebug, methodName, errMsg, paramsVal, success, error) {
		aUtil.aLog.deb(isDebug, this.ApiUrl);
		aUtil.aLog.deb(isDebug, methodName + " paramsVal is " + JSON.stringify(paramsVal));
		var success4Get = function(result, textStatus, xhr) {
			if (!result) {
				aUtil.aLog.err(isDebug, errMsg);
				if (error) error();
			} else {
				aUtil.aLog.deb(isDebug, methodName + " return is " + JSON.stringify(result));
				if (success) {
					success(result);
				} else {
					mui.toast("登录失败");
					if (error) error();
				}
			}
		};
		var urlPath = this.ApiUrl + methodName;
		if (urlPath.indexOf("?") > -1) {
			urlPath = urlPath + "&" + this.parseParam(paramsVal);
		} else {
			urlPath = urlPath + "?" + this.parseParam(paramsVal);
		}

		// paramsVal["jstime"]=new Date().getTime();



		mui.ajax(this.ApiUrl + methodName, {
			data: this.parseParam(paramsVal),
			dataType: 'json', //服务器返回json格式数据
			type: "get",
			timeout: 15000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json',
				'Authorization': "Bearer " + LocalStorage.getItem(LocalStorage.keys.Auth_Token),
			},
			beforeSend: function(xhr, setting) {},
			complete: function(xhr, status) {


			},
			success: success4Get,
			error: function(xhr, type, errorThrown) {
				if (error) error(xhr.responseText);
				console.log(xhr.status + "  " + xhr.readyState + xhr.responseText);
			}
		});

	},

	executeActionOfServerGetNoToken: function(isDebug, methodName, errMsg, paramsVal, success, error) {
		aUtil.aLog.deb(isDebug, this.ApiUrl);
		aUtil.aLog.deb(isDebug, methodName + " paramsVal is " + JSON.stringify(paramsVal));
		var success4Get = function(result, textStatus, xhr) {
			if (!result) {
				aUtil.aLog.err(isDebug, errMsg);
				if (error) error();
			} else {
				aUtil.aLog.deb(isDebug, methodName + " return is " + JSON.stringify(result));
				if (success) {
					success(result);
				} else {
					mui.toast("登录失败");
					if (error) error();
				}
			}
		};
		var urlPath = this.ApiUrl + methodName;
		if (urlPath.indexOf("?") > -1) {
			urlPath = urlPath + "&" + this.parseParam(paramsVal);
		} else {
			urlPath = urlPath + "?" + this.parseParam(paramsVal);
		}

		// paramsVal["jstime"]=new Date().getTime();



		mui.ajax(this.ApiUrl + methodName, {
			data: this.parseParam(paramsVal),
			dataType: 'json', //服务器返回json格式数据
			type: "get",
			timeout: 15000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json',
			},
			beforeSend: function(xhr, setting) {},
			complete: function(xhr, status) {


			},
			success: success4Get,
			error: function(xhr, type, errorThrown) {
				if (error) error(xhr.responseText);
				console.log(xhr.status + "  " + xhr.readyState + xhr.responseText);
			}
		});

	},


	//提交单个（附件）
	executeActionOfServerUpload: function(isDebug, methodName, errMsg, file, postData, success, error) {
		if (!file) {
			plus.nativeUI.alert("没有添加上传文件！");
			return;
		}
		var wt = plus.nativeUI.showWaiting();
		var task = plus.uploader.createUpload(this.ApiUrl + methodName, {
				method: "POST"
			},
			function(t, status) {
				var data = JSON.parse(t.responseText);
				if (status == 200) {
					wt.close();
					if (success && data.code == 0) {
						success(data);
					} else {
						error("上传失败：" + status);
					}
				} else {
					wt.close();
					error("上传失败：" + status);
				}
			}
		);
		task.setRequestHeader("token", LocalStorage.getItem(LocalStorage.keys.Auth_Token));
		task.addData("id", "123");
		task.addData("loginName", "111");
		console.log(file.path);
		task.addFile(file.path, {
			key: "file"
		});
		task.start();
	},
	//提交多个（附件）
	executeActionOfServerUploadMore: function(isDebug, methodName, errMsg, files, postData, success, error) {
		if (!files || files.length == 0) {
			plus.nativeUI.alert("没有添加上传文件！");
			return;
		}
		var wt = plus.nativeUI.showWaiting();
		var task = plus.uploader.createUpload(this.ApiUrl + methodName, {
				method: "POST"
			},
			function(t, status) {
				var data = JSON.parse(t.responseText);
				if (status == 200) {
					wt.close();
					if (success && data.code == 0) {
						success(data);
					} else {
						error("上传失败：" + status);
					}
				} else {
					wt.close();
					error("上传失败：" + status);
				}
			}
		);
		task.setRequestHeader("token", LocalStorage.getItem(LocalStorage.keys.Auth_Token));
		task.addData("id", "123");
		task.addData("loginName", "111");
		for (var i = 0; i < files.length; i++) {
			task.addFile(files[i].path, {
				key: "files"
			});
		}
		task.start();
	},

	parseParam: function(json) {
		if (json) {
			const keys = Object.keys(json);
			let result = '';
			for (let key in keys) {
				result += keys[key] + '=' + json[keys[key]] + '&'
			}
			return result;
		}
		return '';
	},
	ifhaveApi: function() {
		this.ApiUrl = LocalStorage.getItem(LocalStorage.keys.BASEAPI);
		if (this.ApiUrl == null || this.ApiUrl.length == 0) {

			return false;
		} else {
			return true;
		}
	},
	imageURL: function(url) {
		return this.ApiUrl + JSON.stringify(url);
	},

	enterLogin: function() {
		util.login_out();
		setTimeout(function() {
			plus.webview.close(aVariable.webview.current, "none", 0);
		}, 400);
		mui.openWindow({
			id: 'login',
			url: '_www/views/sys/login/login.html',
			show: {
				aniShow: 'slide-in-bottom'
			},
			waiting: {
				autoShow: false
			}
		});
	},
	executeActionOfServerGetRegister: function(isDebug, Url, errMsg, paramsVal, success, error) {
		aUtil.aLog.deb(isDebug, Url);
		aUtil.aLog.deb(isDebug, Url + " paramsVal is " + JSON.stringify(paramsVal));
		var success4Get = function(result, textStatus, xhr) {
			if (!result) {
				aUtil.aLog.err(isDebug, errMsg);
				if (error) error();
			} else {
				aUtil.aLog.deb(isDebug, Url + " return is " + JSON.stringify(result));
				if (success) {
					success(result);
				} else {
					mui.toast("登录失败");
					if (error) error();
				}
			}
		};
		var urlPath = Url;
		if (urlPath.indexOf("?") > -1) {
			urlPath = urlPath + "&" + this.parseParam(paramsVal);
		} else {
			urlPath = urlPath + "?" + this.parseParam(paramsVal);
		}
		
		mui.ajax(Url, {
			data: this.parseParam(paramsVal),
			dataType: 'json', //服务器返回json格式数据
			type: "get",
			timeout: 15000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json',
			},
			beforeSend: function(xhr, setting) {},
			complete: function(xhr, status) {
			},
			success: success4Get,
			error: function(xhr, type, errorThrown) {
				if (error) error(xhr.responseText);
				console.log(xhr.status + "  " + xhr.readyState + xhr.responseText);
			}
		});
	
	}
}
